

#include "sleprandomdata.h"
#include <QGridLayout>
#include <QMessageBox>
#include "rng.h"

SLEPRandomDataWizard::SLEPRandomDataWizard(SLEPItem &item, QWidget *parent)
    :QDialog(parent), localItem(item){
    QGridLayout *layout = new QGridLayout;
    labelType = new QLabel(tr("Type of data"), this);
    buttonGroupType = new QButtonGroup(this);
    buttonGroupType->addButton(new QRadioButton(tr("Dense Matrix")),0);
    buttonGroupType->addButton(new QRadioButton(tr("Sparse Matrix")),1);
    buttonGroupType->addButton(new QRadioButton(tr("Dense Vector")),2);
    buttonGroupType->addButton(new QRadioButton(tr("Sparse Vector")),3);
    buttonGroupType->addButton(new QRadioButton(tr("Single Value")),4);
    labelDimension = new QLabel(tr("Dimension"), this);
    lineEditDimension1 = new QLineEdit(tr("1000"),this);
    lineEditDimension2 = new QLineEdit(tr("1000"),this);
    labelDensity = new QLabel(tr("Density"),this);
    spinBoxDensity = new QDoubleSpinBox(this);
    spinBoxDensity->setRange(0.01, 0.99);
    spinBoxDensity->setValue(0.4);
    spinBoxDensity->setDecimals(2);
    spinBoxDensity->setSingleStep(0.1);
    okay = new QPushButton(tr("Ok"), this);
    cancel = new QPushButton(tr("Cancel"), this);

    layout->addWidget(labelType, 0, 0, 5, 1, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(buttonGroupType->button(0), 0, 1, 1, 2, Qt::AlignLeft);
    layout->addWidget(buttonGroupType->button(1), 1, 1, 1, 2, Qt::AlignLeft);
    layout->addWidget(buttonGroupType->button(2), 2, 1, 1, 2, Qt::AlignLeft);
    layout->addWidget(buttonGroupType->button(3), 3, 1, 1, 2, Qt::AlignLeft);
    layout->addWidget(buttonGroupType->button(4), 4, 1, 1, 2, Qt::AlignLeft);

    buttonGroupType->setExclusive(true);
    (buttonGroupType->button(0))->setChecked(true);
    connect(buttonGroupType, SIGNAL(buttonClicked(int)), this, SLOT(selectionChanged(int)));
    layout->addWidget(labelDimension, 5, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(lineEditDimension1, 5, 1, 1, 1, Qt::AlignLeft);
    layout->addWidget(lineEditDimension2, 5, 2, 1, 1, Qt::AlignRight);
    layout->addWidget(labelDensity, 6, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(spinBoxDensity, 6, 1, 1, 1, Qt::AlignLeft);
    layout->addWidget(okay, 7, 1, 1, 1, Qt::AlignCenter);
    layout->addWidget(cancel, 7, 2, 1, 1, Qt::AlignCenter);
    spinBoxDensity->setDisabled(true);

    connect(okay, SIGNAL(clicked()),this, SLOT(actOnOkay()));
    connect(cancel, SIGNAL(clicked()),this, SLOT(actOnCancel()));

    this->setLayout(layout);
    this->setWindowTitle(tr("Random data"));
    this->setWindowModality(Qt::ApplicationModal);
}




SLEPRandomDataWizard::~SLEPRandomDataWizard(){
}


void SLEPRandomDataWizard::selectionChanged(int i){
    switch(i){
    case 0:
        lineEditDimension1->setText(tr("1000"));
        lineEditDimension1->setEnabled(true);
        lineEditDimension2->setText(tr("1000"));
        lineEditDimension2->setEnabled(true);
        spinBoxDensity->setEnabled(false);
            return;
    case 1:
            lineEditDimension1->setText(tr("1000"));
            lineEditDimension1->setEnabled(true);
        lineEditDimension2->setText(tr("1000"));
        lineEditDimension2->setEnabled(true);
        spinBoxDensity->setEnabled(true);
            return;
    case 2:
            lineEditDimension1->setText(tr("1000"));
            lineEditDimension1->setEnabled(true);
        lineEditDimension2->setText(tr("1"));
        lineEditDimension2->setEnabled(false);
        spinBoxDensity->setEnabled(false);
            return;
    case 3:
            lineEditDimension1->setText(tr("1000"));
            lineEditDimension1->setEnabled(true);
        lineEditDimension2->setText(tr("1"));
        lineEditDimension2->setEnabled(false);
        spinBoxDensity->setEnabled(true);
            return;
    case 4:
            lineEditDimension1->setText(tr("1"));
            lineEditDimension1->setEnabled(false);
            lineEditDimension2->setText(tr("1"));
            lineEditDimension2->setEnabled(false);
        spinBoxDensity->setEnabled(false);
            return;
    }
}

void SLEPRandomDataWizard::actOnOkay(){
    int selected = buttonGroupType->checkedId();
    switch(selected){
    case 0:
        populateRandMatrix();
        break;
    case 1:
        populateRandSparseMatrix();
        break;
    case 2:
        populateRandVector();
        break;
    case 3:
        populateRandSparseVector();
        break;
    case 4:
        populateRandSingleValue();
        break;
    }
    accept();
}


void SLEPRandomDataWizard::actOnCancel(){
    localItem.type = SLEPItem::NONE;
    reject();
}



void SLEPRandomDataWizard::populateRandMatrix(){
    localItem.type = SLEPItem::DENSE_MATRIX;
    bool isOk;
    localItem.m = lineEditDimension1->text().toInt(&isOk);
    if(!isOk){
        localItem.type = SLEPItem::NONE;
        return;
    }
    localItem.n = lineEditDimension2->text().toInt(&isOk);
    if(!isOk){
        localItem.type = SLEPItem::NONE;
        return;
    }
    localItem.data = new double[localItem.m * localItem.n];
    SlepRandom::slep_randn(localItem.data, localItem.m * localItem.n);
}


void SLEPRandomDataWizard::populateRandSparseMatrix(){
    QMessageBox::information(0,QString("Not Supported"),QString("Sparse Matrix Generation is not yet supported"),QMessageBox::Ok);
}

void SLEPRandomDataWizard::populateRandVector(){
    localItem.type = SLEPItem::DENSE_VECTOR;
    bool isOk;
    localItem.dimension = lineEditDimension1->text().toInt(&isOk);
    if(!isOk){
        localItem.type = SLEPItem::NONE;
        return;
    }
    localItem.data = new double[localItem.dimension];
    SlepRandom::slep_randn(localItem.data, localItem.dimension);
}

void SLEPRandomDataWizard::populateRandSparseVector(){
    QMessageBox::information(0,QString("Not Supported"),QString("Sparse Matrix Generation is not yet supported"),QMessageBox::Ok);
}

void SLEPRandomDataWizard::populateRandSingleValue(){
    localItem.type = SLEPItem::SINGLE_VALUE;
    SlepRandom::slep_randn( &(localItem.singlevalue), 1);
}

